home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 3: Developer Tools / Linux Cubed Series 3 - Developer Tools.iso / utils / disk-man / macforma.0 / macforma / macformat / macformat.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-29  |  2.4 KB  |  101 lines

  1. /***********************************************************
  2. *  macformat.c -- write a mac filesystem on a floppy disk  *
  3. *----------------------------------------------------------*
  4. *  ⌐1995 Artsoft Development                               *
  5. *        Holger Schemel                                    *
  6. *        33659 Bielefeld-Senne                             *
  7. *        Telefon: (0521) 493245                            *
  8. *        eMail: aeglos@valinor.owl.de                      *
  9. *               aeglos@uni-paderborn.de                    *
  10. *               q99492@pbhrzx.uni-paderborn.de             *
  11. ***********************************************************/
  12.  
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <fcntl.h>
  16.  
  17. #include "global.h"
  18. #include "block1.h"
  19. #include "block2.h"
  20.  
  21. #define MIN(a,b)        (((a)<(b))?(a):(b))
  22. #define MAX(a,b)        (((a)>(b))?(a):(b))
  23.  
  24. #define MAX_LABELLENGTH    27
  25. #define DEF_EMPTYLABEL    "Empty"
  26.  
  27. void SetDiskLabel(char *);
  28. void WriteDiskBlocks(char *, unsigned char *, int, int);
  29.  
  30. int main(int argc, char **argv)
  31. {
  32.   char progname[1024];
  33.  
  34.   strcpy(progname,GetFilename(argv[0]));
  35.  
  36.   if (argc<2 || argc>3 ||
  37.       argc==2 && (!strcmp(argv[1],"-?") || !strcmp(argv[1],"-h")))
  38.   {
  39.     fprintf(stderr,"Usage: %s <disk device> [volume label]\n",progname);
  40.     exit(-1);
  41.   }
  42.  
  43.   SetDiskLabel(argc==3 ? argv[2] : DEF_EMPTYLABEL);
  44.  
  45.   WriteDiskBlocks(argv[1],block1_bytes,BLOCKOFFSET1,BLOCKCOUNT1);
  46.   WriteDiskBlocks(argv[1],block2_bytes,BLOCKOFFSET2,BLOCKCOUNT2);
  47.  
  48.   exit(0);
  49. }
  50.  
  51. void SetDiskLabel(char *label)
  52. {
  53.   int i, len;
  54.   unsigned char *ptr;
  55.   int label_position[][2] =
  56.   {
  57.     1, 0x0424,
  58.     1, 0x3614,
  59.     1, 0x367c,
  60.     2, 0x0024
  61.   };
  62.  
  63.   for(i=0;i<4;i++)
  64.   {
  65.     ptr = (label_position[i][0] == 1 ? block1_bytes : block2_bytes);
  66.     ptr += label_position[i][1];
  67.  
  68. #ifdef DEBUG
  69.     {
  70.       char oldlabel[MAX_LABELLENGTH+1];
  71.  
  72.       len = *ptr;
  73.       strncpy(oldlabel,ptr+1,MIN(len,MAX_LABELLENGTH));
  74.  
  75.       printf("%d. Stelle: LΣnge = %d, Inhalt = '%s'\n",i+1,len,oldlabel);
  76.     }
  77. #endif
  78.  
  79.     len = *ptr++ = MIN(strlen(label),MAX_LABELLENGTH);
  80.     strncpy(ptr,label,len);
  81.   }
  82. }
  83.  
  84. void WriteDiskBlocks(char *device,unsigned char *blocks, int offset, int count)
  85. {
  86.   int fd_device;
  87.  
  88.   if ((fd_device=open(device,O_WRONLY))<0)
  89.   {
  90.     perror(device);
  91.     exit(-1);
  92.   }
  93.   lseek(fd_device, offset*BLOCKSIZE, SEEK_SET);
  94.   if (write(fd_device, blocks, count*BLOCKSIZE)!=count*BLOCKSIZE)
  95.   {
  96.     perror(device);
  97.     exit(-1);
  98.   }
  99.   close(fd_device);
  100. }
  101.